﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TicketsPhone.Web.Models;
using TicketsPhone.Domain.Access;
using TicketsPhone.Domain.Entities;
using System.Web.Security;
using TicketsPhone.Entidades;


namespace TicketsPhone.Web.Controllers
{
    public class HomeController : Controller
    {

        UsuarioRepositorio _usuario = new UsuarioRepositorio();

        public HomeController()
        {
            
        }
        
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(AutenticacionModel model)
        {
            
            if (ModelState.IsValid)
            {
               Usuario us = _usuario.ValidarAutenticacion(model.Username,  Utilidades.Utilidades.MD5(model.Pass));
               if (us.IdUsuario.HasValue)
               {
                   model.EsUsuarioValido = true;
                   FormsAuthentication.SetAuthCookie(string.Format("{0}|{1}|{2} {3}|{4}|{5}|{6}", us.IdUsuario, us.Email, us.Nombre, us.Apellidos, us.Entidad.IdEntidad, us.Perfil.IdPerfil, us.IdSucursal.Value), true);
                   return RedirectToAction("Modulos");
               }
               else
               {
                   model.EsUsuarioValido = false;
               }
            }

            return View(model);
        }

        public ActionResult Modulos()
        {
            return View(new BaseModel());
        }

        public ActionResult Salir()
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
